package org.geeks.gsmybatis.mapper;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.IntStream;

import org.geeks.gsmybatis.GsMybatisApplicationTests;
import org.geeks.gsmybatis.biz.enums.Gender;
import org.geeks.gsmybatis.biz.mapper.UserMapper;
import org.geeks.gsmybatis.biz.model.User;
import org.geeks.gsmybatis.biz.vo.UserVo;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

@Slf4j
public class UserMapperT extends GsMybatisApplicationTests {

	@Autowired
	private UserMapper userMapper;

	@Test
	public void insert() {
		User user = new User();
		user.setGender(Gender.MALE);
		user.setAge(17);
		LocalDateTime localDateTime = LocalDateTime.now();
		user.setCreateTime(localDateTime);
		user.setUpdateTime(localDateTime);
		user.setName("🤡 Geek-S 🤡");

		userMapper.insert(user);

		log.info("User info : {}", user);
	}

	@Test
	public void insertBatch() {
		List<User> users = Lists.newArrayList();

		IntStream.range(1, 7).forEach(i -> {
			User user = new User();
			user.setGender(Gender.MALE);
			user.setAge(17);
			user.setCamelNaming("Geek-S =>" + i);
			LocalDateTime localDateTime = LocalDateTime.now();
			user.setCreateTime(localDateTime);
			user.setUpdateTime(localDateTime);
			user.setName("Geek-S => " + i);

			users.add(user);
		});

		userMapper.insertList(users);

		log.info("User info : {}", users);
	}

	@Test
	public void selectByExample() {
		Example example = new Example(User.class);
		example.and().andEqualTo("gender", Gender.MALE);

		userMapper.selectByExample(example).forEach(user -> {
			log.info("User info : {}", user);
		});
	}

	@Test
	public void selectAll() {
		userMapper.selectAll().forEach(user -> {
			log.info("User info : {}", user);
		});
	}

	@Test
	public void selectAllByPage() {
		Integer page = 1;

		Integer size = 7;

		PageInfo<User> users = PageHelper.startPage(page, size).doSelectPageInfo(() -> userMapper.selectAll());

		users.getList().forEach(user -> {
			log.info("User info : {}", user);
		});
	}

	@Test
	public void deleteByExample() {
		Example example = new Example(User.class);
		example.and().andIsNull("gender");

		userMapper.deleteByExample(example);

		log.info("Mission complete ~");
	}

	@Test
	public void selectByGenderByAnnotation() {
		Gender gender = Gender.MALE;

		List<User> users = userMapper.selectByGender(gender);

		users.forEach(user -> {
			log.info("User info : {}", user);
		});
	}

	@Test
	public void selectByCondition() {

		String name = "Geek-S";

		Gender gender = Gender.MALE;

		Integer age = 17;

		List<User> users = userMapper.selectByCondition(Strings.isNullOrEmpty(name) ? null : name + "%", gender, age);

		users.forEach(user -> {
			log.info("User info : {}", user);
		});
	}

	@Test
	public void selectByConditionBySqlProvider() {

		String name = "Geek-S";

		Gender gender = Gender.MALE;

		Integer age = 17;

		List<UserVo> users = userMapper.selectByConditionBySqlProvider(Strings.isNullOrEmpty(name) ? null : name + "%",
				gender, age);

		users.forEach(user -> {
			log.info("User info : {}", user);
		});
	}

	@Test
	public void updateByPrimaryKeySelective() {
		Long id = 17L;

		String name = "Geek-S => 17 Update";

		User user = new User();
		user.setId(id);
		user.setName(name);

		userMapper.updateByPrimaryKeySelective(user);

		log.info("Mission complete ~");
	}

	@Test
	public void updateByExample() {
		String name = "Geek-S => 17 MALE to FEMALE";

		User user = new User();

		user.setName(name);

		Example example = new Example(User.class);
		example.and().andEqualTo("gender", Gender.MALE);

		userMapper.updateByExampleSelective(user, example);

		log.info("Mission complete ~");
	}
}
